package com.scu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scu.bank.model.dao.ScienceDynamicDao;
import com.scu.bank.model.example.ScienceDynamicExample;
import com.scu.bank.model.pojo.AcademicActivitie;
import com.scu.bank.model.pojo.ScienceDynamic;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.model.pojo.UserInfo;
import com.scu.bank.service.ScienceDynamicService;
import com.scu.bank.service.UserInfoService;
import com.scu.bank.util.ListUtils;
import com.scu.bank.util.VerificationUtil;
import com.scu.bank.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by pmeng on 2021/01/05.
 */
@Service
@Transactional
public class ScienceDynamicServiceImpl extends BaseServiceImpl<ScienceDynamic,ScienceDynamicDao> implements ScienceDynamicService {
    @Autowired
    private ScienceDynamicDao scienceDynamicDao;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public ScienceDynamicDao getDao() {
        return scienceDynamicDao;
    }

    @Override
    public PageInfo<ScienceDynamic> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        ScienceDynamicExample example = new ScienceDynamicExample();
        example.createCriteria().andIsDelEqualTo(false);
        PageInfo pageInfo = new PageInfo(scienceDynamicDao.selectByExample(example));

        List<ScienceDynamic> list = pageInfo.getList();

        if (!ListUtils.isEmpty(list)){
            list.forEach((temp)->{
                UserInfo userInfo = userInfoService.getById(temp.getPublishUser());
                temp.setPublishUserName(userInfo == null ? "" : userInfo.getUserName());
            });
        }
        pageInfo.setList(list);

        return pageInfo;
    }

    @Override
    public void addScienceDynamic(ScienceDynamic scienceDynamic) {
        scienceDynamic.setStatus("0");
        scienceDynamic.setPublishUser(WebUtil.getCurrentUserId());
        save(scienceDynamic);
    }

    @Override
    public void targetScienceDynamic(String id) {
        ScienceDynamic scienceDynamic = getById(id);
        VerificationUtil.checkServiceBusiness(scienceDynamic == null,"数据不存在!");
        scienceDynamic.setStatus("1".equals(scienceDynamic.getStatus()) ? "0" : "1");
        update(scienceDynamic);
    }

    @Override
    public List<ScienceDynamic> getScienceDynamic() {
        return scienceDynamicDao.getScienceDynamic();
    }

}
